package com.psjj.privilege.po;

import java.util.List;

/**
 * @author : 陈磊
 * @date : 2021-11-15
 * @desc : 用户表
 */
public class User {
    /** 用户主键 */
    private Integer uid ;
    /** 用户名 */
    private String username ;
    /** 手机号 */
    private String phone ;
    /** 用户状态【1代表可用】【0代表禁用】 */
    private int status ;
    /** 用户密码 */
    private String pwd;
    /*关联角色信息*/
    private List<Role> roles;
    //所有受到权限控制的url
    private List<String> urls;

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }

    //查看当前用户是否拥有此权限
    public boolean hasPrivilegeByName(String name){
        //管理员拥有所有权限不用判断
        if("admin".equals(this.username)){
            return true;
        } else {
            boolean hasPrivilege = false;
            //获得当前用户的所有角色信息
            List<Role> roles = this.getRoles();
            //遍历角色
            for(Role role : roles){
                //获得角色所拥有的权限
                List<Privilege> privileges = role.getPrivileges();
                for(Privilege privilege : privileges){
                    //获取用户拥有的权限名字
                    String menuname = privilege.getMenuname();
                    if(menuname.equals(name)){
                        hasPrivilege = true;
                    }
                }
            }
            return hasPrivilege ;
        }
    }
    //判断用户是否拥有此访问路径的访问权限
    public boolean hasPrivilegeByUrl(String url) {
        //管理员可以访问任何访问路径
        if ("admin".equals(this.username)) {
            return true;
        }
        //判断url是否是权限控制的url
        boolean tag = false;
        for(String u : this.urls){
            if(url.startsWith(u)){
                tag = true;
            }
        }
        boolean hasUrl = false;
        //如果url受到权限控制，就判单是否有这个权限，如果url不受权限控制直接返回true
        if(tag){
            //获得当前用户的所有角色信息
            List<Role> roles = this.getRoles();
            //遍历角色
            for(Role role : roles){
                //获得角色所拥有的权限
                List<Privilege> privileges = role.getPrivileges();
                for(Privilege privilege : privileges){
                    //获取当前用户拥有访问权限的所有路径
                    String purl = privilege.getUrl();

                    if(purl!=null && url.startsWith(purl)){
                        hasUrl = true;
                    }
                }
            }
        } else {
            return true;
        }
        return hasUrl;

    }
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", phone='" + phone + '\'' +
                ", status=" + status +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}